<template>
    <!-- 病例管理-门诊记录（原：用户病例） -->
    <div class="health-table">
        <yx-table :is-fit="false" :table-data="tableData" :columns="columns" :page-obj="pageObj" :loading="loading" :tableHeightDefault="600">
            <template slot="patientSex" slot-scope="scope">
                <div>{{scope.scope.patientSex | sexFilter}}</div>
            </template>
            <template slot="price" slot-scope="scope">
                <div>{{ scope.scope.userType == 'Z' || scope.scope.userType == 'YD' ? '/' : scope.scope.price }}</div>
            </template>
            <template slot="operate" slot-scope="scope">
                <div class="operate-btn">
                    <div>
                        <el-button style="color: #006DB8;" size="small" type="text" @click="viewCase(scope.scope)">
                            查看病历
                        </el-button>
                    </div>
                    <!-- 更改为静默签 -->
                    <!-- <div v-if="$global.isCa && scope.scope.isNeedUpdate == 1 && !['Z','YD'].includes(scope.scope.userType) ">
                        <el-button style="color: #006DB8;" size="small" type="text" @click="updateCase(scope.scope.logId)">
                            更新病历
                        </el-button>
                    </div> -->
                </div>
            </template>
        </yx-table>

        <!-- 查看病历组件 -->
        <case-history ref="caseHistory"></case-history>
    </div>
</template>

<script>
    import yxTable from '../../../../components/yxtable/yx-table'
    import caseHistory from '@/components/case-history';

    export default {
        components: {
            yxTable,
            caseHistory,
        },
        props: {
            queryList: {
                type: Object,
                default () {
                    return {}
                }
            },
        },
        data() {
            return {
                columns: [
                    { label: '问诊患者', prop: 'patientName' },
                    { label: '性别', slot: 'patientSex' },
                    { label: '年龄', prop: 'patientAge' },
                    { label: '联系电话', prop: 'patientPhone' },
                    { label: '诊断', prop: 'diagnosisDescribe' },
                    { label: '用户来源', prop: 'userSource' },
                    { label: '问诊日期', prop: 'acceptsTime' },
                    // { label: '处方状态', prop: 'prescriptionStatus' },
                    { label: '未开原因', prop: 'unprescribedReasons' },
                    { label: '问诊费用', slot: 'price' },
                    { label: '操作', slot: 'operate', width: 100 },
                ], // 操作列
                loading: false,
                pageObj: {
                    size: 10,
                    total: 0,
                    currentPage: 1,
                    func: (currentPage) => {
                        this.pageTurning(currentPage)
                    },
                    handleSizeChange: (size) => {
                        this.sizeChange(size)
                    }
                },
                tableData: [],
            }
        },
        mounted() {
            this.getUserRecords();
        },
        methods: {
            viewCase(e) {
                this.$nextTick(() => {
                    if (this.$global.platform('lj')) {
                        this.$refs.caseHistory.onOpen('his', e)
                    } else {
                        if (this.$global.isNewCase) {
                            this.$refs.caseHistory.onOpen('ca', e);
                        } else {
                            this.$refs.caseHistory.onOpen('normal', {
                                caseInfo: e,
                                idCard: e.patientIdcard
                            });
                        }
                    }
                })
            },
            // 数据查询
            queryData() {
                this.getUserRecords();
            },
            pageTurning(currentPage) {
                this.pageObj.currentPage = currentPage;
                this.getUserRecords();
            },
            sizeChange(size) {
                this.pageObj.size = size;
                this.getUserRecords();
            },
            getUserRecords() {
                this.loading = true;
                let params = {
                    current: this.pageObj.currentPage,
                    size: this.pageObj.size,
                    endDate: this.queryList.time[1],
                    startDate: this.queryList.time[0],
                    patientName: this.queryList.patientName,
                    patientPhone: this.queryList.patientPhone
                }
                this.$ajax.getUserRecords(params).then(res => {
                    this.loading = false;
                    let { code, data } = res;
                    if (code === 1) {
                        this.tableData = data.list
                        this.pageObj.total = data.total
                    }
                }).catch(err => {
                    this.loading = false;
                });
            },
            async updateCase(logId) {
                let tempWindow = null;
                if (await this.$util.notSilentCa()) {
                    tempWindow = window.open('', '_blank');
                    tempWindow.document.title = 'e签宝(加载中,请勿关闭)'
                }

                this.$ajax.updateUserCaCase({
                    inquiryLogId: logId
                }).then(res => {
                    this.gotoNewWin(tempWindow, res.data.esignUrl)
                    this.getUserRecords();
                    this.$util.message(res.msg, 'success')
                }).catch(err => {
                    tempWindow && tempWindow.close();
                    this.$util.message(err.msg)
                })
            },
            gotoNewWin(tempWindow, url = '') {
                if (!tempWindow) return;
                if (url) {
                    tempWindow.location.href = url;
                } else {
                    tempWindow && tempWindow.close();
                }
            }
        }
    }
</script>

<style lang="scss">
    .health-table {
        /deep/ .el-button--small {
            padding: 5px 15px;
        }
    }
</style>